/**
 * 
 */
package com.gson.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import com.gson.model.ChResultInfo;
import com.gson.model.OrgInfo;


/**
 * @作者 Stone
 * @创建日期 2013-11-25 下午2:43:00
 * @版本 V1.0
 * @类说明
 */
public class TestParseXml {

	private static final String FILE_NAME = "/result.xml";
	private static final String FILE_IN = "D:\\test.json";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(readString4());
		
		String result = readString4();
		
		SaxParseUtil sax = new SaxParseUtil();
		ChResultInfo resultInfo = sax.getSearchResult(result);
		List<OrgInfo> list = resultInfo.getOrgList();
		
		for (OrgInfo orgInfo : list) {
			System.out.println(orgInfo.getCustomerName());
			System.out.println(orgInfo.getAddress());
			System.out.println(orgInfo.getCustTelephone());
		}

	}

	/* 貌似是说网络环境中比较复杂，每次传过来的字符是定长的，用这种方式？ */
	// public String readString1()
	// {
	// try
	// {
	// //FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流，请考虑使用 FileReader。
	// FileInputStream inStream=this.openFileInput(FILE_NAME);
	// ByteArrayOutputStream bos = new ByteArrayOutputStream();
	// byte[] buffer=new byte[1024];
	// int length=-1;
	// while( (length = inStream.read(buffer) != -1)
	// {
	// bos.write(buffer,0,length);
	// // .write方法 SDK 的解释是 Writes count bytes from the byte array buffer
	// starting at offset index to this stream.
	// // 当流关闭以后内容依然存在
	// }
	// bos.close();
	// inStream.close();
	// return bos.toString();
	// // 为什么不一次性把buffer得大小取出来呢？为什么还要写入到bos中呢？ return new(buffer,"UTF-8") 不更好么?
	// // return new String(bos.toByteArray(),"UTF-8");
	// }
	// }

	/*
	 *  有人说了 FileReader  读字符串更好，那么就用FileReader吧
	 */
	private static String readString2() {
		StringBuffer str = new StringBuffer("");
		File file = new File(FILE_IN);
		try {
			FileReader fr = new FileReader(file);
			int ch = 0;
			while ((ch = fr.read()) != -1) {
				str.append((char)ch); 
			}
			fr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("File reader出错");
		}
		return str.toString();
	}

	/* 按字节读取字符串 */
	/* 个人感觉最好的方式，（一次读完）读字节就读字节吧，读完转码一次不就好了 */
	private static String readString3() {
		String str = "";
		File file = new File(FILE_IN);
		try {
			InputStream in = TestParseXml.class.getClassLoader().getResourceAsStream(FILE_NAME);
			//FileInputStream in = new FileInputStream(file);
			// size 为字串的长度 ，这里一次性读完
			int size = in.available();
			byte[] buffer = new byte[size];
			in.read(buffer);
			in.close();
			str = new String(buffer, "UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
		return str;
	}

	/* InputStreamReader+BufferedReader读取字符串 ， InputStreamReader类是从字节流到字符流的桥梁 */
	/* 按行读对于要处理的格式化数据是一种读取的好方式 */
	private static String readString4() {
		int len = 0;
		StringBuffer str = new StringBuffer("");
		File file = new File(FILE_IN);
		try {
			//FileInputStream is=new FileInputStream(classPath + FILE_NAME);
			String classPath = FileUtil.class.getResource("/").getPath().replaceAll("%20", " ");
			FileInputStream is=new FileInputStream(classPath + FILE_NAME);
            InputStreamReader isr= new InputStreamReader(is);
			BufferedReader in = new BufferedReader(isr);
			String line = null;
			while ((line = in.readLine()) != null) {
				if (len != 0) // 处理换行符的问题
				{
					str.append("\r\n" + line);
				} else {
					str.append(line);
				}
				len++;
			}
			in.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str.toString();
	}

}
